'''
Case Type： 故障&可靠性测试
Case Name： 主节点表文件删除权限
Create At: 2020/07/20
@zou_jialiang0505328114
Descption:
步骤 1.启动主备数据库，判断数据库状态；
步骤 2.手动删除主机的表文件权限未删除备机的表文件权限；
步骤 3.重启主备机（主备机任意一台执行重启）；
History:
Modified by @wan005 2020/11/12 :修改用例，解决概率性失败问题，增加等待刷新脏页和checkpoint操作，并优化脚本结构
'''

import os
import unittest
import _thread
import time
from yat.test import macro
from yat.test import Node
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Deletaduit(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    commonshsta = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case191.py 开始执行------------')
        self.dbPrimaryDbUser = Node(node='PrimaryDbUser')
        self.dbStandby1DbUser = Node(node='Standby1DbUser')
        self.Constant = Constant()
        self.common = Common('PrimaryRoot')
        self.tblname = 'reliability_dbsys191'
        self.tblpath = ''
        self.valuenum = 100000

    def test_startdb(self):
        self.log.info('-------------------创建数据表，并插入数据-----------------------------------')
        result = self.common.createLargeNumTable(self.dbPrimaryDbUser, self.tblname, 1, self.valuenum)
        self.log.info(result)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, result)
        self.assertIn(f'INSERT 0 {self.valuenum}', result)

        self.log.info('----------------------------查询表文件路径----------------------')
        o_id = self.commonshpri.executDbSql(f"select oid from pg_class where relname='{self.tblname}';")
        self.log.info(o_id)
        oid = o_id.splitlines()[-2]
        self.log.info(oid)
        result = self.commonshpri.executDbSql(f'Select pg_relation_filepath({oid}::regclass);')
        self.log.info(result)
        indexpath_tmp = result.splitlines()[-2].strip()
        self.tblpath = os.path.join(macro.DB_INSTANCE_PATH, indexpath_tmp)
        self.log.info(f'table path is: {self.tblpath}')

        self.log.info('------------------------等待脏页刷盘-------------------')
        result = self.commonshpri.check_whether_remain_dirty_page()
        self.assertTrue(result)

        self.log.info('---------------------------执行checkpoint-----------------------------')
        result = self.commonshpri.executDbSql('checkpoint')
        self.log.info(result)

        self.log.info('-------------------主机修改对应表文件权限--------------------')
        chmodcmd = 'chmod 000 ' + self.tblpath
        self.log.info(chmodcmd)
        self.dbPrimaryDbUser.sh(chmodcmd)

        chmodcmd = 'ls -lh ' + self.tblpath
        self.log.info(chmodcmd)
        msg = self.dbPrimaryDbUser.sh(chmodcmd).result()
        self.log.info(msg)

        self.log.info('-------------------重启数据库-------------------------------')
        time.sleep(10)
        self.log.info('查询数据库状态')
        status = self.commonshpri.getDbClusterStatus("status")
        self.log.info(status)
        result = self.commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)

        status = self.commonshpri.getDbClusterStatus("status")
        self.log.info(status)
        time.sleep(10)

        chmodcmd = 'ls -lh ' + self.tblpath
        self.log.info(chmodcmd)
        msg = self.dbPrimaryDbUser.sh(chmodcmd).result()
        self.log.info(msg)

        self.log.info('-------------------查询主备数据库对应表数据-------------------')
        sql = f'select count(*) from {self.tblname};'
        result = self.commonshpri.executDbSql(sql)
        self.log.info(result)
        self.assertIn(self.Constant.PERMISSION_DENIED, result)
        result = self.commonshsta.executDbSql(sql)
        self.log.info(result)
        self.assertIn(str(self.valuenum), result)

        self.log.info('-------------------主机恢复对应表文件权限--------------------')
        chmodcmd = 'chmod 600 ' + self.tblpath
        self.log.info(chmodcmd)
        self.dbPrimaryDbUser.sh(chmodcmd)

        self.log.info('-------------------查询主备数据库对应表数据-------------------')
        sql = f'select count(*) from {self.tblname};'
        result = self.commonshpri.executDbSql(sql)
        self.log.info(result)
        self.assertIn(str(self.valuenum), result)
        result = self.commonshsta.executDbSql(sql)
        self.log.info(result)
        self.assertIn(str(self.valuenum), result)

    def tearDown(self):
        self.log.info('-------------------------this is tearDown------------------------')
        self.log.info('-------------------主机恢复对应表文件权限--------------------')
        chmodcmd = 'chmod 600 ' + self.tblpath
        self.log.info(chmodcmd)
        self.dbPrimaryDbUser.sh(chmodcmd)

        self.log.info('-------------------重启数据库-------------------------------')
        result = self.commonshpri.stopDbCluster()
        result = self.commonshpri.startDbCluster()

        self.log.info('------------------drop table------------------------')
        result = self.commonshpri.executDbSql(f'drop table if exists {self.tblname} ;')
        self.log.info(result)
        self.log.info('-----------------------reliability_dbsys191.py 执行完成------------------------------------')
